package data;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import analyse.RecommandOnTime;

import util.TimeUtil;
import dataconnect.ConnectionFactory;

/**
 * 装载历史及当前数据
 * @author doyl
 *
 */
public class LoadData {
	public static LoadHistoryData lhd = new LoadHistoryData();
	public static Map<String,TimeStock> stockList = new Hashtable<String,TimeStock>();
	private static Set<String> keySet = null;
	
	public static Map<String, TimeStock> getStockList() {
		return stockList;
	}

	public void loadAllData() {
		
		Statement stmt= null;
		ResultSet rs = null;
		Map<String, String> industryMap = new HashMap<String, String>();
		Map<String, String> sinaIndustryMap = new HashMap<String, String>();
		try {
			String sql = "select * from stockinfo";
			Connection con = ConnectionFactory.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				industryMap.put(rs.getString("number"), rs.getString("industry"));
				sinaIndustryMap.put(rs.getString("number"), rs.getString("sinaIndustry"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		HistoryData hd = lhd.loadData();
		keySet = hd.keySet();
		
//		CmbReaschHunter crh = new CmbReaschHunter();
//		crh.start();
		long t1 = System.currentTimeMillis();
		StockDataFetch.refreshData(keySet);
		System.out.println("fetch all finished:"+(System.currentTimeMillis()-t1)+"ms");
		
		RefreshData rd = new RefreshData(keySet);
		rd.start();
		
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String code = iterator.next();
			TimeStock ts = new TimeStock();
			ts.setLastStock(hd.get(code));
			ts.setCode(code);
			if(industryMap.get(code)==null){
				ts.setIndustry("");
			}else{
				ts.setIndustry(industryMap.get(code));
			}
			
			if(sinaIndustryMap.get(code)==null){
				ts.setSinaIndustry("");
			}else{
				ts.setSinaIndustry(sinaIndustryMap.get(code));
			}
//			ts.start();
			stockList.put(code,ts);
		}
		
		TimeStockThread tst = new TimeStockThread();
		tst.start();
		
		DealPoint dp = new DealPoint();
		dp.start();
		
		new TimeUtil();
		
		RecommandOnTime rot = new RecommandOnTime();
		rot.start();
		
		SaleAndBuyPool sabp = new SaleAndBuyPool();
		sabp.start();
		
	}
	
	public static String[] timeStockToVector(String code){
		TimeStock ts = stockList.get(code);
		String[] row = new String[10];
		row[0]=code;
		if(ts!=null&&ts.getCurPrice()!=null){
			row[1]=ts.getStockName();
			row[2]=ts.getCurPrice().toString();
			row[3]=ts.getCurPrice().subtract(ts.getPreDayPrice()).toString();
			BigDecimal increPriceRate = ((ts.getCurPrice().subtract(ts.getPreDayPrice())).divide(ts.getPreDayPrice(),4,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal("100")).setScale(2);
			row[4]=increPriceRate+"%";
			row[5]=ts.getAmount().divide(new BigDecimal("10000"), 2, BigDecimal.ROUND_HALF_UP).toString();
			row[6]=ts.getTime().toString();
			row[7]=ts.getMaxPrice().toString();
			row[8]=ts.getMinPrice().toString();
			row[9]=ts.getIndustry();
		}
		return row;
	}
	
	public static TimeStock getTimeStock(String code){
		return stockList.get(code);
	}
	
}
